<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../sale/jquery-autocomplete/lib/jquery.js"></script>
<script type="text/javascript" src="../sale/jquery-autocomplete/jquery.autocomplete.js"></script>
<script type="text/javascript" src="../sale/jquery-autocomplete/lib/jquery.bgiframe.min.js"></script>
<script type="text/javascript" src="../sale/jquery-autocomplete/lib/jquery.ajaxQueue.js"></script>
<script type="text/javascript" src="../sale/jquery-autocomplete/lib/thickbox-compressed.js"></script>

<link rel="stylesheet" href="../sale/jquery-autocomplete/jquery.autocomplete.css"/>

<?php include("menu.php"); ?>
<script>
function setFocus(){
document.formadd.name.focus();
}
</script>
  <?php
include("../connect/connect.php");

$Query = "SELECT prod_name FROM product";
$objQuery = mysql_query($Query) or die("Error Query [" . $Query . "]");
	while($row = mysql_fetch_assoc($objQuery)){
		$line1[]=array($row["prod_name"]);
	}
?> 
<script type="text/javascript">
$(document).ready(function(){

	var str_month = <?php echo json_encode($line1).';'; ?>

	$("#name").autocomplete( str_month , {
		mustMatch: false,
		autoFill: false,
		matchContains:true,
		});

})
</script>
</head>
<body onload="setFocus()">
<?php

if($_GET["action"]==add) {

	if($_POST["code"]!="" && $_POST["name"]!="" && $_POST["price"]!="" && $_POST["category_id"]!="" && $_POST["rop"]!="")
		{
		
		$checkcode = "SELECT prod_barcode,prod_name FROM product WHERE prod_barcode = '".$_POST["code"]."' OR prod_name = '".$_POST["name"]."'";
		$result = mysql_query($checkcode) or die ("'".checkcode."'");
		$row = mysql_num_rows($result);
		
		if($row>=1){
			$save="รหัสสินค้า ".$_POST["code"]." หรือชื่อสินค้า ".$_POST["name"]." นี้มีอยู่แล้วกรุณาใช้รหัสหรือชื่ออื่น";
			} else {
			$Query = "INSERT INTO product (prod_barcode,prod_name,prod_price,prodtype_id,prod_rop) VALUES ";
			$Query .="('".$_POST["code"]."','".$_POST["name"]."','".$_POST["price"]."','".$_POST["category_id"]."','".$_POST["rop"]."' )";
			$objQuery = mysql_query($Query);

			if($objQuery) {
				$save="บันทึกข้อมูลเรียบร้อยแล้ว";
				} else {
				echo "Error Save [".$Query."]";
				}
			}
		} else {
		$save="กรุณากรอกข้อมูลให้ครบ";
		}
}
?>
<form action="?action=add" name="formadd" method="post" id="formadd">
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading"><h1>เพิ่มสินค้า</h1>
    <div class="buttons"><a onclick="formadd.submit();" class="button"><span>บันทึก</span></a><a onclick="location = 'product_manage.php';" class="button"><span>ยกเลิก</span></a></div></div>
  <div class="content">
    <table class="form" id="form">
      <tr>
        <td>รหัสสินค้า : </td>
        <td><input type="text" value="<?php 
        $mQuery = mysql_query("SELECT max(prod_barcode) as prod_barcode FROM product");
        $mRow = mysql_fetch_array($mQuery);
        echo $mRow['prod_barcode']+1;
         ?>" name="code" id="code" readonly="readonly" /></td>
      </tr>
      <tr>
        <td>ชื่อสินค้า : </td>
        <td><input type="text" name="name" id="name" size="30" /></td>
      </tr>
      <tr>
        <td>หมวดสินค้า : </td>
        <td><select name="category_id" id="category_id">
                <option value=""><-- Please Select --></option>
                <?php
			$Querycate = "SELECT * FROM producttype ORDER BY prodtype_id ASC";
			$objQuerycate = mysql_query($Querycate);
			$strNum=0;
			while($row = mysql_fetch_array($objQuerycate))
			{
			$strNum++;
			?>
                <option value="<?php echo $row["prodtype_id"];?>">
                <?php echo $strNum." - ".$row["prodtype_name"];?>
                </option>
                <?php
			}
			?>
              </select></td>
      </tr>
      <tr>
        <td>ราคาขาย : </td>
        <td><input type="text" name="price" id="price" onkeydown="if(event.keyCode==13){formadd.submit();}" /></td>
      </tr>
      <tr>
        <td>จุดสั่งซื้อ : </td>
        <td><input type="text" value="0" name="rop" id="rop" onkeydown="if(event.keyCode==13){formadd.submit();}" /></td>
      </tr>
    </table>
	<?php echo "<br><font color=\"red\"><center>$save</center></font>"; ?>
  </div>
</div>
</div>
</form>
<?php mysql_close(); ?>
</body>
</html>
